<template >
    <div>
        <!-- mian bao xie  -->
        <div class="mbx">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
                <el-breadcrumb-item>用户列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
            <!-- table  -->
            <div class="tab-box">
                <!-- search -->
                <div class="search">
                    <el-input placeholder="请输入内容" v-model="page.query" class="input-with-select">
                        <el-button slot="append" icon="el-icon-search" @click="searchs"></el-button>
                    </el-input>
                    <el-button type="primary" @click="adduser">添加用户</el-button>
                </div>
                <!-- table  -->
                <div class="tab">
                    <el-table :data="tableData" border style="width: 100%">
                        <el-table-column fixed type="index" label="#">
                        </el-table-column>
                        <el-table-column prop="username" label="姓名" width="170">
                        </el-table-column>
                        <el-table-column prop="email" label="邮箱" width="170">
                        </el-table-column>
                        <el-table-column prop="mobile" label="电话" width="170">
                        </el-table-column>
                        <el-table-column prop="role_name" label="角色" width="170">
                        </el-table-column>
                        <el-table-column prop="zip" label="状态" width="170">

                            <!-- kai guan ca cao  -->
                            <template slot-scope="scope">
                                <el-switch @change="open(scope.row)" v-model="scope.row.mg_state" active-color="#13ce66"
                                    inactive-color="#ff4949">
                                </el-switch>
                            </template>

                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="180">
                            <template slot-scope="scope">
                                <el-button type="primary" icon="el-icon-edit" size="mini"
                                    @click="revise(scope.row)"></el-button>
                                <el-button type="danger" icon="el-icon-delete" size="mini"
                                    @click="del(scope.row)"></el-button>
                                <el-button type="warning" icon="el-icon-s-tools" size="mini"
                                    @click="role(scope.row)"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!-- 分页  -->
                <div class="block">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="page.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="page.pagesize"
                        layout="total, sizes, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                </div>
                <!-- add and revise模态框 -->
                <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
                    <!-- biao dan  -->
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="ruleForm.username" :disabled="title == 'revise-user'"></el-input>
                        </el-form-item>

                        <el-form-item label="密码" prop="password" v-show="title == 'add-user'">
                            <el-input v-model="ruleForm.password"></el-input>
                        </el-form-item>

                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="ruleForm.email"></el-input>
                        </el-form-item>

                        <el-form-item label="手机" prop="mobile">
                            <el-input v-model="ruleForm.mobile"></el-input>
                        </el-form-item>


                        <el-form-item>
                            <p class="p-btn">
                                <el-button @click="resetForm('ruleForm')">取消</el-button>
                                <el-button type="primary" @click="submitForm('ruleForm')"
                                    v-show="title == 'add-user'">确定</el-button>
                                <el-button type="primary" @click="reviseok('ruleForm')"
                                    v-show="title == 'revise-user'">ok</el-button>
                            </p>
                        </el-form-item>
                    </el-form>




                </el-dialog>

                <!-- role 模态框  -->
                <el-dialog title="分配角色" :visible.sync="dialogFormVisible">
                    <div class="p-top">
                        <p>当前的用户:{{ form.username }}</p>
                        <p>当前用户的角色:{{ form.role_name }}</p>
                       <p>
                        分配新角色：
                        <el-select v-model="value" placeholder="请选择">
                            <el-option v-for="item in roles" :key="item.id" :label="item.roleName" :value="item.id">
                            </el-option>
                        </el-select>
                       </p>

                    </div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="usrtroles">确 定</el-button>
                    </div>
                </el-dialog>







            </div>
       
    </div>
</template>
<script>
import { del } from "vue";
import { getusers, getadd, getrevisechange, getrevise, getdel, getusersroles, getroles } from "../api/api";
export default {
    data() {
        return {
            // table 
            tableData: [],

            //分页参数
            page: {
                query: "",  // 查询参数 
                pagenum: 1,    // 当前页码
                pagesize: 10	//每页显示条数
            },
            total: 0,//总条数
            //模态框标题文字
            title: "",
            // 表单数据
            ruleForm: {},
            //表单验证
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    // { min: 3, max: 10, message: '用户名的长度在 3 到 10个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    // { min: 6, max: 15, message: '密码的长度在 6到 15 个字符', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    // { type: 'email', message: '请输入正确的邮箱', trigger: ['blur', 'change'] }
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    // { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入合法手机号', trigger: 'blur' }
                ],
            },
            //模态框显示
            dialogVisible: false,
            //revise id
            reviseid: "",
            //role 模态框出现
            dialogFormVisible: false,
            //role 的数据
            form: {},
            //juese
            roles: [],
            value:'',
            rolesId:0,
        }
    },
    methods: {
        //huo取数据
        getgoods() {
            getusers({ params: this.page }).then(res => {
                console.log(res);
                this.tableData = res.data.users
                this.total = res.data.total
                console.log(this.total);

            })
        },
        //每页显示条数
        handleSizeChange(val) {
            // console.log(val);
            this.page.pagesize = val
            this.getgoods()
        },
        //跳转页数
        handleCurrentChange(val) {
            //  console.log(val);
            this.page.pagenum = val
            this.getgoods()
        },
        //点击添加出现模态框
        adduser() {
            this.dialogVisible = true
            this.title = 'add-user'
            this.ruleForm = {}
        },
        //xiu gai zhuang tai
        open(row) {
            getrevisechange(row).then(res => {
                // console.log(res);
                this.getgoods()
            })
        },
        //点击修改出现模态框
        revise(row) {
            this.dialogVisible = true
            this.ruleForm = row
            this.title = "revise-user"
            this.reviseid = row.id
            // console.log(this.reviseid);
        },
        //点击确定进行添加
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // console.log('submit!');
                    //添加接口
                    getadd(this.ruleForm).then(res => {
                        // console.log(res);

                        this.getgoods()
                        this.dialogVisible = false
                    })

                } else {
                    // console.log('error submit!!');
                    return false;
                }
            });
        },
        //表单重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false
        },
        //点击reviseok修改内容
        reviseok() {
            getrevise(this.ruleForm).then(res => {
                //   console.log(res);
                this.getgoods()
                this.dialogVisible = false
            })
        },

        //点击del进行删除
        del(row) {
            // console.log(id);
            getdel(row).then(res => {
                console.log(res);
                this.getgoods()
            })
        },
        //搜索
        searchs() {
            this.getgoods()
        },

        //点击role出现模态框
        role(row) {
            this.dialogFormVisible = true
            this.form = row
            this.rolesId=row.id
            getroles().then(res => {
                console.log(res);
                this.roles = res.data.map((item, index) => {
                    return {
                        id: item.id,
                        roleName: item.roleName,

                    }
                })

            })
        },
        usrtroles() {
            getusersroles({id:this.rolesId,rid:this.value}).then(res=>{
                console.log(res);
                this.getgoods()
            })

        }
    },
    created() {
        //获取用户信息列表
        this.getgoods()
    },
}
</script>
<style lang="scss" scoped>
.tab-box {
    margin-top: 15px;
    background-color: #fff;
    padding: 18px;
    box-sizing: border-box;
}

.search {
    margin-bottom: 15px;
}

.input-with-select {
    width: 350px;
    margin-right: 18px;
}

.p-btn {
    text-align: right;
}

.p-top {
    p {
        margin: 8px 0;
    }

    margin-bottom: 8px;
}
</style>